//
//  MusicHomePageVC.m
//  彩虹儿歌
//
//  Created by SXS on 15/7/2.
//  Copyright (c) 2015年 HelloBaby. All rights reserved.
//

#import "MusicHomePageVC.h"
#import "MacroHeader.h"
#import "RequestManager.h"
#import "MusicViewModels.h"
#import "MusicTableViewCell.h"
#import <UIImageView+WebCache.h>

@interface MusicHomePageVC ()<UITableViewDataSource,UITableViewDelegate,RequestDelegate>

@property (nonatomic ,retain)RequestManager *request;
@property (nonatomic ,retain)UITableView *mainTableView;
@property (nonatomic ,retain)NSMutableArray *dataSourceArr;
@property (nonatomic ,retain)UIView *footerView;
@property (nonatomic ,retain)MusicViewModels *models;

@end

@implementation MusicHomePageVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIImageView *backgroundImage= [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"xiaohua01"]];
    [self.view addSubview:backgroundImage];
    self.dataSourceArr = [NSMutableArray array];
    [self requestData];
    [self p_setupMainTableView];
//    [self createFooterView];
    
    [backgroundImage release];
}

#pragma mark -tableView自定义

- (void)p_setupMainTableView
{
    self.mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStylePlain];
    self.mainTableView.delegate = self;
    self.mainTableView.dataSource = self;
    self.mainTableView.rowHeight = (SCREEN_HEIGHT - 64)/5;
    self.mainTableView.backgroundColor = [UIColor clearColor];
    self.mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.mainTableView registerClass:[MusicTableViewCell class] forCellReuseIdentifier:@"musicCell"];
    [self.view addSubview:self.mainTableView];
    [self.mainTableView release];
                                                             
}


#pragma mark - 代理方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.dataSourceArr count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MusicViewModels *model = self.dataSourceArr[indexPath.row];
    MusicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"musicCell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = model;
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *clearV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 60)];
    clearV.backgroundColor = [UIColor clearColor];
    return clearV;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 60;
}

#pragma mark - 数据请求

- (void)requestData
{
    self.request = [[RequestManager alloc] initWithRequestURL:@"http://app.9nali.com/847?page_id=1&device=iPhone&version=1.1.5" requestModel:GET parameter:nil];
    self.request.delegate = self;
    [self.request startRequest];
    [self.request release];
}

- (void)requestDidFinishWithData:(NSData *)data
{
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers |NSJSONReadingMutableLeaves error:nil];
    NSArray *mArr = dic[@"list"];
    for (NSDictionary *dict in mArr) {
        MusicViewModels *model = [[MusicViewModels alloc] init];
        [model setValuesForKeysWithDictionary:dict];
        [self.dataSourceArr addObject:model];
        [model release];
    }
    [self.mainTableView reloadData];
}

- (void)requestError:(NSError *)error
{
    NSLog(@"==%@",error);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
